<?php
class KatalogTAModel{	
	protected $lokalizacja;
	
	protected function zapiszUstawieniaSklepu(){
		global $config,$dispatcher;
		try{
			foreach ($config->ustawienia_katalogu_ta as $id => $wartosc) {
				$id__ = tools::__adodb('getOne',"SELECT id  FROM ".TASETTIN." WHERE id = ".tools::__mres($id),true,$this->db); 
				if($id == $id__){
					tools::__adodb("exec","UPDATE ".TASETTIN." SET wartosc = ".tools::__mres($wartosc)." WHERE id = ".tools::__mres($id),true,$this->db);
				}else{
					tools::__adodb("exec","INSERT INTO ".TASETTIN." (id, wartosc) VALUES (".tools::__mres($id).",".tools::__mres($wartosc).");",true,$this->db);
				}
			}
		} catch (PDOException $e) {
			$dispatcher->Panel_Komunikaty->err = $e->getMessage();
		}
	}	
	
	/**
	 * ustawienia, konfiguracja sklepu
	 * @return unknown_type
	 */
	protected function pobiezUstawienia(){
		global $dispatcher;
		try{
			$q__ = "SELECT id,wartosc FROM ".TASETTIN;
			$result = tools::__adodb('getAssoc',$q__,true,$this->db);
			return $result;
		} catch (PDOException $e) {
			$dispatcher->Panel_Komunikaty->err = $e->getMessage();
		}
	}
	
	/**
	 * zapis do bazy nowej kategorii
	 * @return unknown_type
	 */
	protected function zapiszNowaKategorieDoBazy(){
		global $config;
		
		
		
		$q__ = "SELECT MAX(tacLp) FROM ".TACATEGO." WHERE tacpID = ".tools::__mres($config->tacpID);
		$lp__ = tools::__adodb('getOne',$q__,true,$this->db);
		$lp__++;
		
		tools::__adodb("exec","BEGIN;");
		
		$q__ = "INSERT INTO ".TACATEGO." (tacID,tacpID,tacStatus,tacLp,tacStan,tacActiveLevel) 
			VALUES (NULL,".tools::__mres($config->tacpID).",0,{$lp__},0,".tools::__mres($config->tacActiveLevel).")";
		tools::__adodb('exec',$q__,true,$this->db);		
		
		$this->tacID = $this->db->lastInsertId();
	
					
		if(is_array($config->tacNazwa)){
			foreach ($config->tacNazwa as $lang => $tacNazwa__) {
				$tacOPis__ = (isset($config->tacOpis[$lang])) ? $config->tacOpis[$lang] : '';
				$q__ = "INSERT INTO ".TACATELA." (tacID,tacLang,tacNazwa,tacOpis) 
					VALUES (".tools::__mres($this->tacID).",'{$lang}',".tools::__mres($tacNazwa__).",".tools::__mres($this->lokalizacja).")";
				tools::__adodb('exec',$q__,true,$this->db);	

				$this->lokalizacja = "";
				$this->strukturaWybranejKategorii($this->tacID);
				$q__ = "UPDATE ".TACATELA." SET tacOpis = ".tools::__mres($this->lokalizacja)." 
					WHERE tacID = '{$this->tacID}' AND tacLang = ".tools::__mres($lang);
				tools::__adodb('exec',$q__,true,$this->db);	
		
			}
		}else{
			$q__ = "INSERT INTO ".TACATELA." (tacID,tacLang,tacNazwa,tacOpis) 
				VALUES (".tools::__mres($this->tacID).",'{$config->__lang}',".tools::__mres($config->tacNazwa).",".tools::__mres($this->lokalizacja).")";
			tools::__adodb('exec',$q__,true,$this->db);
		}
		

		tools::__adodb("exec","END;");
							
		return $this->tacID;
	}
	
	protected function zapiszNowaPozycjeSlownikaPom1DoBazy(){
		global $config;
		$q__ = "SELECT MAX(tas1Lp) FROM ".TAKUCHNI." WHERE tas1pID = ".tools::__mres($config->tas1pID);
		$lp__ = tools::__adodb('getOne',$q__,true,$this->db);
		$lp__++;
		$q__ = "INSERT INTO ".TAKUCHNI." (tas1ID,tas1pID,tas1Status,tas1Lp,tas1Stan) 
			VALUES (NULL,".tools::__mres($config->tas1pID).",0,{$lp__},0)";
		tools::__adodb('exec',$q__,true,$this->db);		
		
		$this->tas1ID = $this->db->lastInsertId();
		
		if(is_array($config->tas1Nazwa)){
			foreach ($config->tas1Nazwa as $lang => $tas1Nazwa__) {
				$q__ = "INSERT INTO ".TAKUCHLA." (tas1ID,tas1Lang,tas1Nazwa) 
					VALUES (".tools::__mres($this->tas1ID).",'{$lang}',".tools::__mres($tas1Nazwa__).")";
				tools::__adodb('exec',$q__,true,$this->db);				
			}
		}else{
			$q__ = "INSERT INTO ".TAKUCHLA." (tas1ID,tas1Lang,tas1Nazwa) 
				VALUES (".tools::__mres($this->tas1ID).",'{$config->__lang}',".tools::__mres($config->tas1Nazwa).")";
			tools::__adodb('exec',$q__,true,$this->db);
		}
		return $this->tas1ID;	
	}
	
	protected function pobiezDaneSlownikaPom1zBazy($tas1ID=null){
		global $objSession,$config;
		if(is_null($tas1ID)){
			// lista wszystkich kategorii dla domyslnego jezyka
			if ( $config->__logg  && $config->__stan == 'a'){			
				$q__ = "SELECT a.*, b.tas1Nazwa, b.tas1Lang FROM ".TAKUCHNI." a 
					LEFT JOIN ".TAKUCHLA." b ON b.tas1ID = a.tas1ID 
					WHERE b.tas1Lang = '{$config->__lang}'  ORDER BY a.tas1Lp";
			}else{
				$q__ = "SELECT a.*, b.tas1Nazwa, b.tas1Lang FROM ".TAKUCHNI." a 
					LEFT JOIN ".TAKUCHLA." b ON b.tas1ID = a.tas1ID 
					WHERE b.tas1Lang = '{$config->__lang}' 
					AND a.tas1Status = 0 	ORDER BY a.tas1Lp"; 				
			}
			return tools::__adodb('getAll',$q__,true,$this->db);
		}else{
			if  ($lang != null){
				// kategoria dla wybranego jezyka
				$q__ = "SELECT a.*, b.tas1Nazwa, b.tas1Lang FROM ".TAKUCHNI." a 
					LEFT JOIN ".TAKUCHLA." b ON b.tas1ID = a.tas1ID 
					WHERE b.tas1Lang = '{$lang}' AND a.tas1ID = '$tas1ID'";
				return tools::__adodb('getRow',$q__,true,$this->db);
			}else{
				// kategoria wiele jezykow
				$q__ = "SELECT a.*, b.tas1Nazwa, b.tas1Lang FROM ".TAKUCHNI." a 
					LEFT JOIN ".TAKUCHLA." b ON b.tas1ID = a.tas1ID 
					WHERE a.tas1ID = '$tas1ID'";
				return tools::__adodb('getAll',$q__,true,$this->db);							
			}
		}
	}
	
	protected function pobiezDaneSlownikaPom1zBazyDlaProduktu($tapID=null,$tacID=null){
		global $config;
		$q__ = "SELECT a.*, b.tas1Nazwa, b.tas1Lang FROM ".TAKUCHNI." a 
					LEFT JOIN ".TAKUCHLA." b ON b.tas1ID = a.tas1ID 
					WHERE b.tas1Lang = '{$config->__lang}' 
					AND a.tapID  ORDER BY a.tas1Lp";
        $q__ = "SELECT ".TAKUCHLA.".*
				FROM ".TAKUCHLA."
					INNER JOIN ".TAKUCHNI." ON (".TAKUCHLA.".tas1ID=".TAKUCHNI.".tas1ID)
					INNER JOIN ".TAPRODKU." ON (".TAPRODKU.".tas1ID=".TAKUCHNI.".tas1ID)
				WHERE  ".TAPRODKU.".tacID = '{$tacID}' 
					AND ".TAPRODKU.".tapID = '{$tapID}' 
					AND ".TAKUCHLA.".tas1Lang = '{$config->__lang}'";
		return tools::__adodb('getAll',$q__,true,$this->db);
	}
	
	
	protected function pobiezDaneSlownikaPom1zZaznaczeniemProduktu($tapID=null,$tacID=null){
		global $config;

        $q__ = "SELECT a.*, 
        			( SELECT 1 FROM ".TAPRODKU." WHERE ".TAPRODKU.".tacID = '{$tacID}' AND ".TAPRODKU.".tapID = '{$tapID}' AND b.tas1ID = ".TAPRODKU.".tas1ID)  AS 'tas1Selected' 
				FROM ".TAKUCHLA." AS a
					LEFT JOIN ".TAKUCHNI." AS b ON (a.tas1ID=b.tas1ID)
				WHERE  a.tas1Lang = '{$config->__lang}'";
		return tools::__adodb('getAll',$q__,true,$this->db);
	}
	
	protected function pobiezDaneOPlikachProduktuKategorii($tapID=null,$tacID=null,$grupa = 0){
		$query = "SELECT ".TAPRODPL.".*
				FROM ".TAPRODPL." WHERE ".TAPRODPL.".tacID = '{$tacID}' AND ".TAPRODPL.".tapID = '{$tapID}'	AND tafGrupa = '{$grupa}' 
 			   ORDER BY ".TAPRODPL.".tafNazwa ";
		$result = tools::__adodb('getAll',$query,true,$this->db);
		return $result;
	}
	
	/**
	 * zapis edytowanej kategorii do bazy
	 * @return unknown_type
	 */
	protected function zapiszEdytowanaKategorieDoBazy(){
		global $config;		
		if(is_array($config->tacNazwa)){
			foreach ($config->tacNazwa as $lang => $tacNazwa__) {
				$q__ = "SELECT COUNT(*) FROM ".TACATEGO." a 
					LEFT JOIN ".TACATELA." b ON b.tacID = a.tacID 
					WHERE b.tacLang = '{$lang}' AND a.tacID = '{$config->tacID}'";
				$istnieje = tools::__adodb('getOne',$q__,true,$this->db);
				if($istnieje == 1){
					
					$this->lokalizacja = "";
					$this->strukturaWybranejKategorii($config->tacID);							
							
					$q__ = "UPDATE ".TACATELA." SET tacNazwa = ".tools::__mres($tacNazwa__).",
						tacOpis = ".tools::__mres($this->lokalizacja)." 
						WHERE tacID = ".tools::__mres($config->tacID)." AND tacLang = ".tools::__mres($lang);
					tools::__adodb('exec',$q__,true,$this->db);	
				}else{
					$q__ = "INSERT INTO ".TACATELA." (tacID,tacLang,tacNazwa,tacOpis) 
					VALUES (".tools::__mres($config->tacID).",'{$lang}',".tools::__mres($tacNazwa__).",".tools::__mres($this->lokalizacja).")";
					tools::__adodb('exec',$q__,true,$this->db);		
				}
			}
			
		}else{
			$q__ = "UPDATE ".TACATELA." SET tacNazwa = ".tools::__mres($config->tacNazwa)."
				WHERE tacID = ".tools::__mres($config->tacID)." AND tacLang = ".tools::__mres($config->tacLang);
			tools::__adodb('exec',$q__,true,$this->db);
		}	

		if(is_array($config->tacOpis)){
			foreach ($config->tacOpis as $lang => $Opis__) {
				$q__ = "SELECT COUNT(*) FROM ".TACATEGO." a 
					LEFT JOIN ".TACATELA." b ON b.tacID = a.tacID 
					WHERE b.tacLang = '{$lang}' AND a.tacID = '{$config->tacID}'";
				$istnieje = tools::__adodb('getOne',$q__,true,$this->db);
				if($istnieje == 1){
					$q__ = "UPDATE ".TACATELA." SET tacOpis = ".tools::__mres($Opis__)."
						WHERE tacID = ".tools::__mres($config->tacID)." AND tacLang = ".tools::__mres($lang);
					tools::__adodb('exec',$q__,true,$this->db);	
				}else{
					$q__ = "INSERT INTO ".TACATELA." (tacID,tacLang,tacOpis) 
					VALUES (".tools::__mres($config->tacID).",'{$lang}',".tools::__mres($Opis__).")";
					tools::__adodb('exec',$q__,true,$this->db);		
				}
			}
		}	
		//inne
		$q__ = "UPDATE ".TACATEGO." SET tacActiveLevel = ".tools::__mres($config->tacActiveLevel)."
			WHERE tacID = ".tools::__mres($config->tacID);
		tools::__adodb('exec',$q__,true,$this->db);
		
	}
	
	/**
	 * pobiera dane kategorii z bazy
	 * @param $tacID
	 * @param $lang
	 * @param $pgStatus
	 * @return unknown_type
	 */
	protected function pobiezDaneKategoriizBazy($tacID = null,$lang=null, $tacStatus=''){
		global $objSession,$config;
		if(is_null($tacID)){
			// lista wszystkich kategorii dla domyslnego jezyka
			if ( $config->__logg/*  && $config->__stan == 'a'*/){			
				$q__ = "SELECT a.*, b.tacNazwa, b.tacLang, b.tacOpis FROM ".TACATEGO." a 
					INNER JOIN ".TACATELA." b ON b.tacID = a.tacID 
					WHERE b.tacLang = '{$config->__lang}'  ORDER BY b.tacNazwa";
			}else{
				$q__ = "SELECT a.*, b.tacNazwa, b.tacLang, b.tacOpis FROM ".TACATEGO." a 
					INNER JOIN ".TACATELA." b ON b.tacID = a.tacID 
					WHERE b.tacLang = '{$config->__lang}' 
					AND a.tacStatus = 0 	ORDER BY b.tacNazwa"; 				
			}
			return tools::__adodb('getAll',$q__,true,$this->db);
		}else{
			if  ($lang != null){
				// kategoria dla wybranego jezyka
				$q__ = "SELECT a.*, b.tacNazwa, b.tacOpis, b.tacLang FROM ".TACATEGO." a 
					LEFT JOIN ".TACATELA." b ON b.tacID = a.tacID 
					WHERE b.tacLang = '{$lang}' AND a.tacID = '$tacID'";
				return tools::__adodb('getRow',$q__,true,$this->db);
			}else{
				// kategoria wiele jezykow
				$q__ = "SELECT a.*, b.tacNazwa, b.tacOpis, b.tacLang FROM ".TACATEGO." a 
					LEFT JOIN ".TACATELA." b ON b.tacID = a.tacID 
					WHERE a.tacID = '$tacID'";
				return tools::__adodb('getAll',$q__,true,$this->db);							
			}
		}
	}
	
	
	protected function pobiezDaneKategoriizBazyNaPoziomy($tacID = null,$lang=null, $tacStatus=''){
		global $objSession,$config;
			$this->subkategorie="";
			$this->pobierzListeSubkategoriiSklepu(0,1,0);
			$sub__ = explode(';',$this->subkategorie);
			$this->subkategorie = '(';
			if (is_array($sub__) && !empty($sub__))
			foreach ($sub__ as $sub__) {	
				if (!empty($sub__)) $this->subkategorie .= "'{$sub__}'".",";			
			}
			$this->subkategorie .= "'{$tacID__}')";
			
			$q__ = "SELECT a.*, b.tacNazwa, b.tacLang, b.tacOpis FROM ".TACATEGO." a 
					INNER JOIN ".TACATELA." b ON b.tacID = a.tacID 
					WHERE b.tacLang = '{$config->__lang}' AND a.tacID IN {$this->subkategorie}";

			return tools::__adodb('getAll',$q__,true,$this->db);
	}
	
	
	protected function pobiezStruktureKategorii( ){
		global $config;

			// lista wszystkich kategorii dla domyslnego jezyka
				$q__ = "SELECT a.tacID, a.tacpID FROM ".TACATEGO." a  
					WHERE a.tacStatus = 0 	ORDER BY a.tacID"; 
			return tools::__adodb('getAll',$q__,true,$this->db);
	}
	
	
	protected function pobiezJednaKategorieDla_tapID($tapID = null,$tacID=null){
		global $config;
		if(!is_null($tapID) && !is_null($tacID)){
			$q__ = "SELECT 
			  takeaway_category.tacID,
			  takeaway_category.tacpID,
			  takeaway_category.tacStatus,
			  takeaway_category.tacLp,
			  takeaway_category.tacStan,
			  takeaway_category.tacActiveLevel,
			  takeaway_category_lang.tacID,
			  takeaway_category_lang.tacLang,
			  takeaway_category_lang.tacNazwa,
			  takeaway_category_lang.tacOpis,
			  takeaway_produkty_categ.tauID
			FROM
			 takeaway_category_lang
			 INNER JOIN takeaway_category ON (takeaway_category_lang.tacID=takeaway_category.tacID)
			 INNER JOIN takeaway_produkty_categ ON (takeaway_produkty_categ.tacID=takeaway_category.tacID)
			 WHERE  takeaway_produkty_categ.tacID = '{$tacID}' 
			 AND takeaway_produkty_categ.tapID = '{$tapID}' 
			 AND takeaway_category_lang.tacLang = '{$config->__lang}'";
			return tools::__adodb('getAll',$q__,true,$this->db);
		}
	}
	
	protected function pobiezJednaDomyslnaKategorie(){
			$q__ = "SELECT tacID FROM  ".TACATEGO." LIMIT 1";
			return tools::__adodb('getOne',$q__,true,$this->db);			
	}	
	
	protected function usunKategorieSklepuZBazy($tacID__){
		$this->pobierzListeSubkategoriiSklepu($tacID__);
		$kategorie_ = explode(';',$this->subkategorie.$tacID__);
		if (!is_array($kategorie_)) return;
		foreach ($kategorie_ as $subCategID) {
			if(is_numeric( $subCategID)){
				
				$query = "SELECT a.tapID, a.tacID FROM ".TAPRODCA." a 
					WHERE a.tacID = ".tools::__mres($subCategID); 

				$oferty_ = tools::__adodb('getAll',$query,true,$this->db);

				tools::__adodb("exec","DELETE FROM ".TACATEGO." WHERE tacID = '{$subCategID}'",true,$this->db);
				tools::__adodb("exec","DELETE FROM ".TACATELA." WHERE tacID = '{$subCategID}'",true,$this->db);				
				
				if (is_array($oferty_))
				foreach ($oferty_ as $key => $produkt) {
					$this->usunProduktSklepuZBazy($produkt['tacID'],$produkt['tapID']);
				}	
			}
		}
	}
	
	
	protected function usunProduktSklepuZBazy($tacID,$tapID){
		$count_ = tools::__adodb('getOne',"SELECT COUNT(*) FROM ".TAPRODCA." WHERE tapID = '{$tapID}'",true,$this->db);
		if ($count_ >= 1){
			tools::__adodb("exec","DELETE FROM ".TAPRODCA." WHERE tapID = '{$tapID}' AND tacID = '{$tacID}'",true,$this->db);
			tools::__adodb("exec","DELETE FROM ".TAPRODKU." WHERE tapID = '{$tapID}' AND tacID = '{$tacID}'",true,$this->db);
			tools::__adodb("exec","DELETE FROM ".TAPRODST." WHERE tapID = '{$tapID}' AND tacID = '{$tacID}'",true,$this->db);
		}
		if($count_ == 1){
			tools::__adodb("exec","DELETE FROM ".TAPRODUK." WHERE tapID = '{$tapID}'",true,$this->db);
			$this->usunLogoProduktu($tapID);
		}
		$this->usunPlikiProduktu($tapID,$tacID);
		$this->_uaktualnij_stan_ofert($tacID,"- 1");
	}
	
	private function usunPlikiProduktu($tapID,$tacID){
		
		$files_ = tools::__adodb('getAll',"SELECT tafID, tapID FROM ".TAPRODPL." WHERE tapID = '{$tapID}' AND tacID = '{$tacID}'",true,$this->db);
		if (is_array($files_)){
			foreach ($files_ as $key => $file) {				
				$curFile = "{$file['tafID']}_photo_ta.jpg";
				$thumbs__ = $this->pobierzListeThumbsDlaPliku($this->folder.$tapID."/",$curFile);
				if(is_array($thumbs__)){;
					foreach ($thumbs__ as $__file){
						tools::_delete_file($__file);
					}
				}		
				tools::_delete_file($this->folder.$tapID."/".$curFile);
			}
		}		
		tools::__adodb("exec","DELETE FROM ".TAPRODPL." WHERE tapID = '{$tapID}' AND tacID = '{$tacID}'",true,$this->db);		
	}
	
	protected function usunJedenPlikProduktu($tafID,$tapID){
		$curFile = "{$tafID}_photo_ta.jpg";
		$thumbs__ = $this->pobierzListeThumbsDlaPliku($this->folder.$tapID."/",$curFile);
		if(is_array($thumbs__)){;
			foreach ($thumbs__ as $__file){
				tools::_delete_file($__file);
			}
		}		
		$result = tools::_delete_file($this->folder.$tapID."/".$curFile);
		if($result[0]) tools::__adodb("exec","DELETE FROM ".TAPRODPL." WHERE tafID = '{$tafID}'",true,$this->db);	
		return $result;
	}
	
	private function usunLogoProduktu($tapID){
		$curFile = "{$tapID}_logo.jpg";
		$thumbs__ = $this->pobierzListeThumbsDlaPliku($this->folder.$tapID."/",$curFile);
		if(is_array($thumbs__)){;
			foreach ($thumbs__ as $__file){
				tools::_delete_file($__file);
			}
		}		
		tools::_delete_file($this->folder.$tapID."/".$curFile);
	}

	protected function pobierzListeThumbsDlaPliku($target,$file){
		global $files;
		$exceptions=array('.','..');
		$pattern="({$file}(.*).thumb$)";
		if (!$sourcedir=@opendir($target)) return false;
		$i=0;
		while(false!==($sibling=readdir($sourcedir))){
			if(!in_array($sibling,$exceptions)){
				$object=str_replace('//','/',$target.'/'.$sibling);
				if(is_dir($object)){
					$this->pobierzListeThumbsDlaPliku($object,$file);
				}
				
				if(is_file($object) && preg_match($pattern,$object)){
					$files[$i] = $object;
				}
				$i++;
			}
		}
		closedir($sourcedir);
		return $files;	
	}
	
	protected function zapiszNowyProduktDoBazy(){
		global $config,$objSession,$dispatcher;
/*	
		Array ( 
		x[tapNazwa] => aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 
		[tacIDs] => Array ( [49] => [50] => ) 
		x[tapLokalizacja] => Array ( [49] => Cork, Cork City, Ballyphehane, [50] => Cork, Cork City, City Centre North, [51] => Cork, Cork City, City Centre South, )
		x[tapMapa] => Array ( [49] => 1 [50] => 1 ) 
		x[tapMenu] => Array ( [49] => 1 [50] => 1 ) 
		x[tapDost] => Array ( [49] => 1 [50] => 1 ) 
		x[tapProm] => Array ( [49] => 1 [50] => 1 ) 
		x[tapAdres] => Array ( [49] => aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa [50] => bbbbbbbbbbbbbbbbbbbbbbbb ) 
		x[lat] => Array ( [49] => 51.8745818 [50] => 51.8992332 ) 
		x[lng] => Array ( [49] => -8.4778593 [50] => -8.4488073 ) 
		x[kuchnia] => Array ( [49] => Array ( [0] => 6 [1] => 7 [2] => 9 [3] => 12 [4] => 15 [5] => 18 [6] => 21 ) [50] => Array ( [0] => 7 [1] => 10 [2] => 13 [3] => 16 [4] => 19 [5] => 22 ) ) 
		[openPonFrom] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[openPonTo] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[deliPonFrom] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[deliPonTo] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[openWtoFrom] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[openWtoTo] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[deliWtoFrom] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[deliWtoTo] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[openSroFrom] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[openSroTo] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[deliSroFrom] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[deliSroTo] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[openCzwFrom] => Array ( [49] => 01:00 [50] => 02:00 )
		[openCzwTo] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[deliCzwFrom] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[deliCzwTo] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[openPioFrom] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[openPioTo] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[deliPioFrom] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[deliPioTo] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[openSobFrom] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[openSobTo] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[deliSobFrom] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[deliSobTo] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[openNieFrom] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[openNieTo] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[deliNieFrom] => Array ( [49] => 01:00 [50] => 02:00 ) 
		[deliNieTo] => Array ( [49] => 01:00 [50] => 02:00 ) 
		x[collection] => Array ( [49] => aaaaaaaaaaaaaaaaaaa [50] => bbbbbbbbbbbbbbbbbbbbbbbbbbb ) 
		x[dellivery] => Array ( [49] => aaaaaaaaaaaaaaaaaaaaaaaaaaa [50] => bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ) )
*/
		if(is_array($config->tacIDs)){	
			$q__ = "INSERT INTO ".TAPRODUK." (tapID,tapStatus,tapNazwa) 
				VALUES (NULL,".tools::__mres($config->tapStatus).",".tools::__mres($config->tapNazwa).")";
			$result = tools::__adodb('exec',$q__,true,$this->db);		
			
			if ($result == 1){ // jesli dodano produkt
				$this->tapID = $this->db->lastInsertId();
				$tauID = $objSession->GetUserData("id");
							
				// zapis produktu do przypisanych kategorii
				foreach ($config->tacIDs as $tacID__ => $id) {
						// wybrany adres			
					$tapAdres = (is_array($config->tapAdres) && array_key_exists($tacID__,$config->tapAdres))?$config->tapAdres[$tacID__]:"";
					$tapDaneEmail = (is_array($config->tapDaneEmail) && array_key_exists($tacID__,$config->tapDaneEmail))?$config->tapDaneEmail[$tacID__]:"";
					$tapDaneTelefon  = (is_array($config->tapDaneTelefon) && array_key_exists($tacID__,$config->tapDaneTelefon))?$config->tapDaneTelefon[$tacID__]:"";
					$tapDaneWww  = (is_array($config->tapDaneWww) && array_key_exists($tacID__,$config->tapDaneWww))?$config->tapDaneWww[$tacID__]:"";
					$tapDaneSkype  = (is_array($config->tapDaneSkype) && array_key_exists($tacID__,$config->tapDaneSkype))?$config->tapDaneSkype[$tacID__]:"";
					$tapMapa = (is_array($config->tapMapa) && array_key_exists($tacID__,$config->tapMapa))?$config->tapMapa[$tacID__]:"0";
					$tapMenu = (is_array($config->tapMenu) && array_key_exists($tacID__,$config->tapMenu))?$config->tapMenu[$tacID__]:"0";
					$tapDost = (is_array($config->tapDost) && array_key_exists($tacID__,$config->tapDost))?$config->tapDost[$tacID__]:"0";
					$tapProm = (is_array($config->tapProm) && array_key_exists($tacID__,$config->tapProm))?$config->tapProm[$tacID__]:"0";
					$tapLoka = (is_array($config->tapLokalizacja) && array_key_exists($tacID__,$config->tapLokalizacja))?$config->tapLokalizacja[$tacID__]:"";
					$tapColl = (is_array($config->collection) && array_key_exists($tacID__,$config->collection))?$config->collection[$tacID__]:"";
					$tapDell = (is_array($config->dellivery) && array_key_exists($tacID__,$config->dellivery))?$config->dellivery[$tacID__]:"";
					$tapPromTxt = (is_array($config->promocje) && array_key_exists($tacID__,$config->promocje))?$config->promocje[$tacID__]:"";
					$tapLat = (is_array($config->lat) && array_key_exists($tacID__,$config->lat))?$config->lat[$tacID__]:"";
					$tapLng = (is_array($config->lng) && array_key_exists($tacID__,$config->lng))?$config->lng[$tacID__]:"";
					
					// godziny
					
					$openPonFrom = (is_array($config->openPonFrom) && array_key_exists($tacID__,$config->openPonFrom))?$config->openPonFrom[$tacID__]:"00:00";
					$openWtoFrom = (is_array($config->openWtoFrom) && array_key_exists($tacID__,$config->openWtoFrom))?$config->openWtoFrom[$tacID__]:"00:00";
					$openSroFrom = (is_array($config->openSroFrom) && array_key_exists($tacID__,$config->openSroFrom))?$config->openSroFrom[$tacID__]:"00:00";
					$openCzwFrom = (is_array($config->openCzwFrom) && array_key_exists($tacID__,$config->openCzwFrom))?$config->openCzwFrom[$tacID__]:"00:00";
					$openPioFrom = (is_array($config->openPioFrom) && array_key_exists($tacID__,$config->openPioFrom))?$config->openPioFrom[$tacID__]:"00:00";
					$openSobFrom = (is_array($config->openSobFrom) && array_key_exists($tacID__,$config->openSobFrom))?$config->openSobFrom[$tacID__]:"00:00";
					$openNieFrom = (is_array($config->openNieFrom) && array_key_exists($tacID__,$config->openNieFrom))?$config->openNieFrom[$tacID__]:"00:00";
					
					$openPonTo = (is_array($config->openPonTo) && array_key_exists($tacID__,$config->openPonTo))?$config->openPonTo[$tacID__]:"00:00";
					$openWtoTo = (is_array($config->openWtoTo) && array_key_exists($tacID__,$config->openWtoTo))?$config->openWtoTo[$tacID__]:"00:00";
					$openSroTo = (is_array($config->openSroTo) && array_key_exists($tacID__,$config->openSroTo))?$config->openSroTo[$tacID__]:"00:00";
					$openCzwTo = (is_array($config->openCzwTo) && array_key_exists($tacID__,$config->openCzwTo))?$config->openCzwTo[$tacID__]:"00:00";
					$openPioTo = (is_array($config->openPioTo) && array_key_exists($tacID__,$config->openPioTo))?$config->openPioTo[$tacID__]:"00:00";
					$openSobTo = (is_array($config->openSobTo) && array_key_exists($tacID__,$config->openSobTo))?$config->openSobTo[$tacID__]:"00:00";
					$openNieTo = (is_array($config->openNieTo) && array_key_exists($tacID__,$config->openNieTo))?$config->openNieTo[$tacID__]:"00:00";
	
					$deliPonFrom = (is_array($config->deliPonFrom) && array_key_exists($tacID__,$config->deliPonFrom))?$config->deliPonFrom[$tacID__]:"00:00";
					$deliWtoFrom = (is_array($config->deliWtoFrom) && array_key_exists($tacID__,$config->deliWtoFrom))?$config->deliWtoFrom[$tacID__]:"00:00";
					$deliSroFrom = (is_array($config->deliSroFrom) && array_key_exists($tacID__,$config->deliSroFrom))?$config->deliSroFrom[$tacID__]:"00:00";
					$deliCzwFrom = (is_array($config->deliCzwFrom) && array_key_exists($tacID__,$config->deliCzwFrom))?$config->deliCzwFrom[$tacID__]:"00:00";
					$deliPioFrom = (is_array($config->deliPioFrom) && array_key_exists($tacID__,$config->deliPioFrom))?$config->deliPioFrom[$tacID__]:"00:00";
					$deliSobFrom = (is_array($config->deliSobFrom) && array_key_exists($tacID__,$config->deliSobFrom))?$config->deliSobFrom[$tacID__]:"00:00";
					$deliNieFrom = (is_array($config->deliNieFrom) && array_key_exists($tacID__,$config->deliNieFrom))?$config->deliNieFrom[$tacID__]:"00:00";
					
					$deliPonTo = (is_array($config->deliPonTo) && array_key_exists($tacID__,$config->deliPonTo))?$config->deliPonTo[$tacID__]:"00:00";
					$deliWtoTo = (is_array($config->deliWtoTo) && array_key_exists($tacID__,$config->deliWtoTo))?$config->deliWtoTo[$tacID__]:"00:00";
					$deliSroTo = (is_array($config->deliSroTo) && array_key_exists($tacID__,$config->deliSroTo))?$config->deliSroTo[$tacID__]:"00:00";
					$deliCzwTo = (is_array($config->deliCzwTo) && array_key_exists($tacID__,$config->deliCzwTo))?$config->deliCzwTo[$tacID__]:"00:00";
					$deliPioTo = (is_array($config->deliPioTo) && array_key_exists($tacID__,$config->deliPioTo))?$config->deliPioTo[$tacID__]:"00:00";
					$deliSobTo = (is_array($config->deliSobTo) && array_key_exists($tacID__,$config->deliSobTo))?$config->deliSobTo[$tacID__]:"00:00";
					$deliNieTo = (is_array($config->deliNieTo) && array_key_exists($tacID__,$config->deliNieTo))?$config->deliNieTo[$tacID__]:"00:00";
					
					/*$this->lokalizacja = "";
					KatalogTAModel::strukturaWybranejKategorii($tacID__);*/
		
					$q__ = "INSERT INTO ".TAPRODCA." (tapID,tacID,tauID, tapAdres, tapDaneEmail, tapDaneTelefon, tapDaneWww, tapDaneSkype, tapLokalizacja,
						tapMapa,tapMenu,tapDost,tapProm,tapCollection, tapDellivery,tapPromotions,tapLat, tapLng,
						openPonFrom, openWtoFrom, openSroFrom, openCzwFrom, openPioFrom, openSobFrom, openNieFrom,
						openPonTo, openWtoTo, openSroTo, openCzwTo, openPioTo, openSobTo, openNieTo,
						deliPonFrom, deliWtoFrom, deliSroFrom, deliCzwFrom, deliPioFrom, deliSobFrom, deliNieFrom,
						deliPonTo, deliWtoTo, deliSroTo, deliCzwTo, deliPioTo, deliSobTo, deliNieTo)										
						 VALUES (".tools::__mres($this->tapID).",'{$tacID__}',
						".tools::__mres($tauID).",
						".tools::__mres($tapAdres).",
						".tools::__mres($tapDaneEmail).",
						".tools::__mres($tapDaneTelefon).",
						".tools::__mres($tapDaneWww).",
						".tools::__mres($tapDaneSkype).",
						".tools::__mres($tapLoka).",
						".tools::__mres($tapMapa).",
						".tools::__mres($tapMenu).",
						".tools::__mres($tapDost).",
						".tools::__mres($tapProm).",
						".tools::__mres($tapColl).",
						".tools::__mres($tapDell).",
						".tools::__mres($tapPromTxt).",
						".tools::__mres($tapLat).",
						".tools::__mres($tapLng).",
						".tools::__mres($openWtoFrom).",
						".tools::__mres($openWtoFrom).",
						".tools::__mres($openSroFrom).",
						".tools::__mres($openCzwFrom).",
						".tools::__mres($openPioFrom).",
						".tools::__mres($openSobFrom).",
						".tools::__mres($openNieFrom).",
						".tools::__mres($openPonTo).",
						".tools::__mres($openWtoTo).",
						".tools::__mres($openSroTo).",
						".tools::__mres($openCzwTo).",
						".tools::__mres($openPioTo).",
						".tools::__mres($openSobTo).",
						".tools::__mres($openNieTo).",
						".tools::__mres($deliPonFrom).",
						".tools::__mres($deliWtoFrom).",
						".tools::__mres($deliSroFrom).",
						".tools::__mres($deliCzwFrom).",
						".tools::__mres($deliPioFrom).",
						".tools::__mres($deliSobFrom).",
						".tools::__mres($deliNieFrom).",
						".tools::__mres($deliPonTo).",
						".tools::__mres($deliWtoTo).",
						".tools::__mres($deliSroTo).",
						".tools::__mres($deliCzwTo).",
						".tools::__mres($deliPioTo).",
						".tools::__mres($deliSobTo).",
						".tools::__mres($deliNieTo)."
						)";
	
					tools::__adodb('exec',$q__,true,$this->db);			
					// uaktualnienie stanow produktow dla kategorii
					$this->_uaktualnij_stan_ofert($tacID__);
					
					// kuchnie
					if (is_array($config->kuchnia) && array_key_exists($tacID__,$config->kuchnia) && is_array($config->kuchnia[$tacID__])){			
						foreach ($config->kuchnia[$tacID__] as $tas1ID__) {
		
							$q__ = "INSERT INTO ".TAPRODKU." (tacID,tapID,tas1ID) 
								VALUES ({$tacID__},".tools::__mres($this->tapID).",'{$tas1ID__}')";
							
							tools::__adodb('exec',$q__,true,$this->db);			
							// uaktualnienie stanow produktow dla kuchni
							//$this->_uaktualnij_stan_ofert($tacID__);
						}
					}
					
					$this->zapiszPlikiProduktuIKategoriiNaDysk($tacID__, $this->tapID);
				}
			}else{				
				throw new Exception($config->__cl["blad_dodawania_nowej_pozycji_w_katalogu_ta"]."!");
			}
		}else{			
			throw new Exception($config->__cl["prosze_najpierw_wybrac_kategorie_dla_nowej_oferty"]);
		}
	}
	
	protected function zapiszLogoProduktuNAaDysk(){
		global $parameters,$dispatcher;
	
		foreach ($_FILES as $fieldName => $file) {

			switch($fieldName){
				case "tapFile":
					// logo					
					if ($file['size'] <= $parameters['KatalogTA']['maxWielkoscPliku'] && $file['error'] == 0 ) {
						$up=move_uploaded_file($file['tmp_name'],  "{$this->folder}{$this->tapID}/{$this->tapID}_logo.jpg");
					}else{
						$dispatcher->Panel_Komunikaty->msg = "Plik jest zbyt duży - max wielkość pliku: {$parameters['KatalogTA']['maxWielkoscPliku']}";				
					}
					break;
			}
		}
	}
	
	
	protected function zapiszPlikiProduktuIKategoriiNaDysk($tacID__, $tapID__){
		global $parameters,$dispatcher,$config;

		$folderZdjec__ = $this->folder.trim($tapID__)."/";
		tools::_add_folder($folderZdjec__);
		
		foreach ($_FILES as $fieldName => $file) {
			switch($fieldName){
				case "pliki":
					// pliki jpg
					foreach ($file["name"] as $tacID => $pliki_name_) {
						if($tacID__ == $tacID){

							
							foreach ($pliki_name_ as $id => $name__) {
								 
								//$tmp__ = $file["tmp_name"][$tacID][$id];
								$tmp__ = (is_array($file["tmp_name"]) && array_key_exists($tacID,$file["tmp_name"]) && array_key_exists($id,$file["tmp_name"][$tacID]) )?$file["tmp_name"][$tacID][$id]:null;
								$err__ = (is_array($file["error"]) && array_key_exists($tacID,$file["error"]) && array_key_exists($id,$file["error"][$tacID]) )?$file["error"][$tacID][$id]:null; 
								$size_ = (is_array($file["size"]) && array_key_exists($tacID,$file["size"]) && array_key_exists($id,$file["size"][$tacID]) )?$file["size"][$tacID][$id]:null; 
								$tafNazwa = (is_array($config->pliki_nazwa) && array_key_exists($tacID,$config->pliki_nazwa) && array_key_exists($id,$config->pliki_nazwa[$tacID]) )?$config->pliki_nazwa[$tacID][$id]:null; 
								$tafTyp = (is_array($file["type"]) && array_key_exists($tacID,$file["type"]) && array_key_exists($id,$file["type"][$tacID]) )?$file["type"][$tacID][$id]:null; $file["type"][$tacID][$id];
 
								//$dispatcher->Panel_Komunikaty->msg = "[{$tacID}]: t[{$tafTyp}] s[{$size_}] e[{$err__}] n[{$tafNazwa}] p[{$tmp__}]";
								
								$q__ = "INSERT INTO ".TAPRODPL." (tafID, tapID,tacID,tafUrl, tafTyp, tafNazwa) 
								VALUES (NULL,'{$tapID__}','{$tacID__}','','{$tafTyp}','{$tafNazwa}')";							

								if($err__ == 0 && $size_ <= $parameters['KatalogTA']['maxWielkoscPliku']){
								
									$result = tools::__adodb('exec',$q__,true,$this->db);							
									if($result == 1){
										$tafID__ = $this->db->lastInsertId();

											$up=move_uploaded_file($tmp__,  "{$folderZdjec__}{$tafID__}_photo_ta.jpg");
										
									}else{
										
										$dispatcher->Panel_Komunikaty->err = $config->__cl["blad_dodawania_pliku_oferty_katalogu_ta"];
									}
								}else{
									
									if(!is_null($tmp__) && !empty($name__)) $dispatcher->Panel_Komunikaty->err = $config->__cl["dodawany_plik_jest_zbyt_duzy_maksymalna_wilekosc_to"].": {$parameters['KatalogTA']['maxWielkoscPliku']}";				
								}										
							}
						}
					}				

					break;
				case "zdjecia":
					// pliki jpg
					foreach ($file["name"] as $tacID => $pliki_name_) {
						if($tacID__ == $tacID){
							foreach ($pliki_name_ as $id => $name__) {
								 
								//$tmp__ = $file["tmp_name"][$tacID][$id];
								$tmp__ = (is_array($file["tmp_name"]) && array_key_exists($tacID,$file["tmp_name"]) && array_key_exists($id,$file["tmp_name"][$tacID]) )?$file["tmp_name"][$tacID][$id]:null;
								$err__ = (is_array($file["error"]) && array_key_exists($tacID,$file["error"]) && array_key_exists($id,$file["error"][$tacID]) )?$file["error"][$tacID][$id]:null; 
								$size_ = (is_array($file["size"]) && array_key_exists($tacID,$file["size"]) && array_key_exists($id,$file["size"][$tacID]) )?$file["size"][$tacID][$id]:null; 
								$tafNazwa = (is_array($config->pliki_nazwa) && array_key_exists($tacID,$config->pliki_nazwa) && array_key_exists($id,$config->pliki_nazwa[$tacID]) )?$config->pliki_nazwa[$tacID][$id]:null; 
								$tafTyp = (is_array($file["type"]) && array_key_exists($tacID,$file["type"]) && array_key_exists($id,$file["type"][$tacID]) )?$file["type"][$tacID][$id]:null; $file["type"][$tacID][$id];
 
								//$dispatcher->Panel_Komunikaty->msg = "[{$tacID}]: t[{$tafTyp}] s[{$size_}] e[{$err__}] n[{$tafNazwa}] p[{$tmp__}]";
								
								$q__ = "INSERT INTO ".TAPRODPL." (tafID, tapID,tacID,tafUrl, tafTyp, tafNazwa, tafGrupa) 
								VALUES (NULL,'{$tapID__}','{$tacID__}','','{$tafTyp}','{$tafNazwa}',1)";							

								if($err__ == 0 && $size_ <= $parameters['KatalogTA']['maxWielkoscPliku']){
								
									$result = tools::__adodb('exec',$q__,true,$this->db);							
									if($result == 1){
										$tafID__ = $this->db->lastInsertId();

											$up=move_uploaded_file($tmp__,  "{$folderZdjec__}{$tafID__}_photo_ta.jpg");
										
									}else{
										
										$dispatcher->Panel_Komunikaty->err = $config->__cl["blad_dodawania_pliku_oferty_katalogu_ta"];
									}
								}else{
									
									if(!is_null($tmp__) && !empty($name__)) $dispatcher->Panel_Komunikaty->err = $config->__cl["dodawany_plik_jest_zbyt_duzy_maksymalna_wilekosc_to"].": {$parameters['KatalogTA']['maxWielkoscPliku']}";				
								}										
							}
						}
					}				

					break;
			}
		}
	}
	
	
	/**
	 * struktura kategorii
	 * @param unknown_type $tacID
	 * @return text
	 */
	protected function strukturaWybranejKategorii($tacID){
		$query_tacpID = "SELECT a.tacID, a.tacpID, b.tacNazwa FROM ".TACATEGO." a LEFT JOIN ".TACATELA." b ON b.tacID = a.tacID WHERE a.tacID = '$tacID'";
		$kategoria = tools::__adodb('getRow',$query_tacpID,true,$this->db);	
		$tacID = (isset($kategoria['tacpID']) && $kategoria['tacpID'] != 0 ) ? $kategoria['tacpID'] : 0;
		if( $tacID != 0 ) $this->strukturaWybranejKategorii($tacID);
		$this->lokalizacja .=  $kategoria['tacNazwa'].", ";
	}
	
	protected function strukturaWybranejKategoriiTablica($tacID){
		$query_tacpID = "SELECT a.tacID, a.tacpID FROM ".TACATEGO." a LEFT JOIN ".TACATELA." b ON b.tacID = a.tacID WHERE a.tacID = '$tacID'";
		$kategoria = tools::__adodb('getRow',$query_tacpID,true,$this->db);	
		$tacID = (isset($kategoria['tacpID']) && $kategoria['tacpID'] != 0 ) ? $kategoria['tacpID'] : 0;
		if( $tacID != 0 ) $this->strukturaWybranejKategoriiTablica($tacID);
		$this->lokalizacja[$kategoria['tacID']] =  $kategoria['tacID'];
	}
	
	/**
	 * polozenie produktu w strukturze kategorii
	 * @param unknown_type $tacID
	 * @return unknown_type
	 */
	protected function strukturaWybranejKategoriiZLinkami($tacID){
		global $config;
		$query_tacpID = "SELECT a.tacID, a.tacpID, b.tacNazwa FROM ".TACATEGO." a LEFT JOIN ".TACATELA." b ON b.tacID = a.tacID WHERE a.tacID = '$tacID'";
		$kategoria = tools::__adodb('getRow',$query_tacpID,true,$this->db);	
		$tacID = (isset($kategoria['tacpID']) && $kategoria['tacpID'] != 0 ) ? $kategoria['tacpID'] : 0;
		if( $tacID != 0 ) $this->strukturaWybranejKategoriiZLinkami($tacID);
		$this->lokalizacja .=  "<a href=\"{$config->page},,show_category,{$kategoria['tacID']}.html\">{$kategoria['tacNazwa']}</a> &raquo; ";
	}
	
	
	/**
	 * uaktualnia wszystkie kategorie nadrzednei i bierzącą o jeden dla Pola tacStan
	 * przy dodaniu oferty
	 * @param $tacID
	 * @return unknown_type
	 */
	private function _uaktualnij_stan_ofert($tacID,$i="+ 1"){
		$q_tacpID = "SELECT tacpID FROM ".TACATEGO." WHERE tacID = '{$tacID}'";
		$tacpID = tools::__adodb('getOne',$q_tacpID,true,$this->db);
		
		$q__ = "UPDATE ".TACATEGO." SET tacStan = tacStan {$i} WHERE tacID = ".tools::__mres($tacID);
		tools::__adodb('exec',$q__,true,$this->db);
		
		if( $tacpID > 0){
			$this->_uaktualnij_stan_ofert($tacpID,$i);
		}else{
			return true;
		}
	}
	
	protected function zapiszEdytowanyProduktDoBazy(){
		global $config;
		$q__0 = "UPDATE ".TAPRODUK." SET tapNazwa = ".tools::__mres($config->tapNazwa)." 
				WHERE tapID = ".tools::__mres($config->tapID);
		$q__1 = "UPDATE ".TAPRODCA." SET 
				tapAdres = ".tools::__mres($config->tapAdres).",
				tapDaneSkype = ".tools::__mres($config->tapDaneSkype).",
				tapDaneTelefon = ".tools::__mres($config->tapDaneTelefon).",
				tapDaneEmail = ".tools::__mres($config->tapDaneEmail).",
				tapDaneWww = ".tools::__mres($config->tapDaneWww).",
				tapMapa = ".tools::__mres($config->tapMapa).",
				tapMenu = ".tools::__mres($config->tapMenu).",
				tapDost = ".tools::__mres($config->tapDost).",
				tapProm = ".tools::__mres($config->tapProm).",
				tapCollection = ".tools::__mres($config->tapCollection).",
				tapDellivery = ".tools::__mres($config->tapDellivery).",
				tapPromotions = ".tools::__mres($config->tapPromotions).",
				tapLat = ".tools::__mres($config->tapLat).",
				tapLng = ".tools::__mres($config->tapLng).",
				openPonFrom = ".tools::__mres($config->openPonFrom).",
				openWtoFrom = ".tools::__mres($config->openWtoFrom).",
				openSroFrom = ".tools::__mres($config->openSroFrom).",
				openCzwFrom = ".tools::__mres($config->openCzwFrom).",
				openPioFrom = ".tools::__mres($config->openPioFrom).",
				openSobFrom = ".tools::__mres($config->openSobFrom).",
				openNieFrom = ".tools::__mres($config->openNieFrom).",
				openPonTo = ".tools::__mres($config->openPonTo).",
				openWtoTo = ".tools::__mres($config->openWtoTo).",
				openSroTo = ".tools::__mres($config->openSroTo).",
				openCzwTo = ".tools::__mres($config->openCzwTo).",
				openPioTo = ".tools::__mres($config->openPioTo).",
				openSobTo = ".tools::__mres($config->openSobTo).",
				openNieTo = ".tools::__mres($config->openNieTo).",
				deliPonFrom = ".tools::__mres($config->deliPonFrom).",
				deliWtoFrom = ".tools::__mres($config->deliWtoFrom).",
				deliSroFrom = ".tools::__mres($config->deliSroFrom).",
				deliCzwFrom = ".tools::__mres($config->deliCzwFrom).",
				deliPioFrom = ".tools::__mres($config->deliPioFrom).",
				deliSobFrom = ".tools::__mres($config->deliSobFrom).",
				deliNieFrom = ".tools::__mres($config->deliNieFrom).",
				deliPonTo = ".tools::__mres($config->deliPonTo).",
				deliWtoTo = ".tools::__mres($config->deliWtoTo).",
				deliSroTo = ".tools::__mres($config->deliSroTo).",
				deliCzwTo = ".tools::__mres($config->deliCzwTo).",
				deliPioTo = ".tools::__mres($config->deliPioTo).",
				deliSobTo = ".tools::__mres($config->deliSobTo).",
				deliNieTo = ".tools::__mres($config->deliNieTo)."									
				WHERE tapID = ".tools::__mres($config->tapID)." AND tacID = ".tools::__mres($config->tacID);		 
				
		$q__2 = "SELECT ".TAPRODKU.".tas1ID, ".TAPRODKU.".tas1ID FROM ".TAPRODKU." 
					WHERE tacID = ".tools::__mres($config->tacID)." AND tapID = ".tools::__mres($config->tapID);
			
					
		tools::__adodb('exec',$q__0,true,$this->db);
		tools::__adodb('exec',$q__1,true,$this->db);
		$ArrTas1ID = tools::__adodb('getAssoc',$q__2,true,$this->db);
		// kuchnie
		if (is_array($config->kuchnia)){			
			foreach ($config->kuchnia as $id => $tas1ID ) {
				if (is_array($ArrTas1ID) && array_key_exists($tas1ID,$ArrTas1ID) ){
					$ArrTas1ID[$tas1ID] = "x";
				}else{
					$q__3 = "INSERT INTO ".TAPRODKU." (tacID,tapID,tas1ID) 
						VALUES (".tools::__mres($config->tacID).",".tools::__mres($config->tapID).",'{$tas1ID}')";
					
					tools::__adodb('exec',$q__3,true,$this->db);	
				}		
			}
		}else{
			$q__4 = "DELETE FROM ".TAPRODKU." WHERE tacID = ".tools::__mres($config->tacID)." AND tapID = ".tools::__mres($config->tapID);
			tools::__adodb('exec',$q__4,true,$this->db);
		}
		if (is_array($ArrTas1ID)){	
			foreach ($ArrTas1ID as $tas1ID  => $val ) {
				if($val != 'x'){
					$q__5 = "DELETE FROM ".TAPRODKU." WHERE tacID = ".tools::__mres($config->tacID)." AND tapID = ".tools::__mres($config->tapID)." AND tas1ID = '{$tas1ID}'";
					tools::__adodb('exec',$q__5,true,$this->db);
				}
			}
		}
		
		$this->zapiszPlikiProduktuIKategoriiNaDysk($config->tacID, $config->tapID);
	}
	
	private function pobierzListeSubkategoriiSklepu($tacID__, $poziom__ = null, $curent__= 0){
		global $config;
		$q__ = "SELECT a.tacID FROM ".TACATEGO." a 
				LEFT JOIN ".TACATELA." b ON b.tacID = a.tacID  
				WHERE tacpID = ".tools::__mres($tacID__)." AND b.tacLang = '{$config->__lang}'";
		$__kategorie = tools::__adodb('getCol',$q__,true,$this->db);
		if (!is_array($__kategorie)) return;
		$this->subkategorie .= join(';',$__kategorie).';';	
		$curent__++;
		if(is_null($poziom__) || !is_null($poziom__) && $curent__ <= $poziom__){
			foreach ($__kategorie as $categID__) {
				$this->pobierzListeSubkategoriiSklepu($categID__,$poziom__, $curent__);
			}
		}
	}

	
	/**
	 * zwraca jeden poziom subkategorii dla wybranej kategorii
	 * @param $tacID__
	 * @return array
	 */
	protected function pobierzListeSubkategoriiJedenPoziom($tacID__){
		global $config;
		$q__ = "SELECT b.* FROM ".TACATEGO." a 
				LEFT JOIN ".TACATELA." b ON b.tacID = a.tacID  
				WHERE tacpID = ".tools::__mres($tacID__)." AND b.tacLang = '{$config->__lang}'";
		$__kategorie = tools::__adodb('getAll',$q__,true,$this->db);
		if (is_array($__kategorie)) return $__kategorie;
		return false;
	}

	
	
	protected function pobierzListeProduktowSklepu($tacID__, $page = null){
	
		$this->subkategorie = '';
		$this->pobierzListeSubkategoriiSklepu($tacID__);
		$sub__ = explode(';',$this->subkategorie);
		$this->subkategorie = '(';
		if (is_array($sub__) && !empty($sub__))
		foreach ($sub__ as $sub__) {	
			if (!empty($sub__)) $this->subkategorie .= "'{$sub__}'".",";			
		}
		$this->subkategorie .= "'{$tacID__}')";
		
		
		$kuchnie = (is_null($this->tas1pID)) ? "" : " AND ".TAPRODUK.".tapID IN (SELECT tapID FROM ".TAPRODKU." WHERE tas1ID = '{$this->tas1pID}') ";
		$kewords = (is_null($this->findkeywords)) ? "" : " AND ".TAPRODUK.".tapNazwa LIKE '%{$this->findkeywords}%' ";
		
		$query = "SELECT COUNT(*)
				FROM ".TAPRODUK." LEFT JOIN ".TAPRODCA." ON (".TAPRODUK.".tapID=".TAPRODCA.".tapID)				
				WHERE (".TAPRODCA.".tacID IN {$this->subkategorie}) {$kuchnie} {$kewords}				 
				ORDER BY ".TAPRODUK.".tapNazwa ".$limity;
		
		$razem =  tools::__adodb('getOne',$query,true,$this->db);
		
		
		$strona_docelowa = (!is_null($page))?$page:1;
		$ilosc_na_strone = 20;
		$zakres_widocznych_stron = 2;
		
		$limity = tools::_stronicowanie($razem,$strona_docelowa,$ilosc_na_strone,$zakres_widocznych_stron);
		
		
		$query = "SELECT 	
				  ".TAPRODUK.".tapNazwa,		  
				  ".TAPRODUK.".tapStatus,
				  ".TAPRODUK.".tapLp,
				  ".TAPRODUK.".tapLogo,
				  ".TAPRODCA.".*
				FROM
				 ".TAPRODUK." LEFT JOIN ".TAPRODCA." ON (".TAPRODUK.".tapID=".TAPRODCA.".tapID)
				WHERE  (".TAPRODCA.".tacID IN {$this->subkategorie}) {$kuchnie} {$kewords}
				   ORDER BY ".TAPRODUK.".tapNazwa ".$limity;
		$result = tools::__adodb('getAll',$query,true,$this->db);	
	
		return $result;
	}

	
	protected function pobiezListeLatLng($tacID=null){
		global $config;
		if (($config->__isset('s1')) && !is_null($config->s1) && !empty($config->s1) ) $this->tas1pID = $config->s1; // indeks kuchni
		if (($config->__isset('s2')) && !is_null($config->s2) && !empty($config->s2)) $this->findkeywords = $config->s2; // slowo kluczowe
		
		if(is_null($tacID)){
			$kuchnie = (is_null($this->tas1pID)) ? "" : " WHERE ".TAPRODUK.".tapID IN (SELECT tapID FROM ".TAPRODKU." WHERE tas1ID = '{$this->tas1pID}') ";
			$w = ( is_null($this->tas1pID)) ? " WHERE " : "AND";
			$findkeywords = (is_null($this->findkeywords)) ? "" : " {$w} ".TAPRODUK.".tapNazwa LIKE '%{$this->findkeywords}%' ";
			
		
			$query = "SELECT 	
					  ".TAPRODUK.".tapNazwa,
					  ".TAPRODCA.".*
					FROM
					 ".TAPRODUK." LEFT JOIN ".TAPRODCA." ON (".TAPRODUK.".tapID=".TAPRODCA.".tapID) {$kuchnie} {$kewords}
					 ";
			$result = tools::__adodb('getAll',$query,true,$this->db);
		
		}else{	

			$kuchnie = (is_null($this->tas1pID)) ? "" : " AND ".TAPRODUK.".tapID IN (SELECT tapID FROM ".TAPRODKU." WHERE tas1ID = '{$this->tas1pID}') ";
			$kewords = (is_null($this->findkeywords)) ? "" : " AND ".TAPRODUK.".tapNazwa LIKE '%{$this->findkeywords}%' ";
			
		
			$this->subkategorie = '';
			$this->pobierzListeSubkategoriiSklepu($tacID);
			$sub__ = explode(';',$this->subkategorie);
			$this->subkategorie = '(';
			if (is_array($sub__) && !empty($sub__))
			foreach ($sub__ as $sub__) {	
				if (!empty($sub__)) $this->subkategorie .= "'{$sub__}'".",";			
			}
			$this->subkategorie .= "'{$tacID}')";
			
			$query = "SELECT 	
				  ".TAPRODUK.".tapNazwa,
				  ".TAPRODCA.".*
				FROM
				 ".TAPRODUK." LEFT JOIN ".TAPRODCA." ON (".TAPRODUK.".tapID=".TAPRODCA.".tapID)
				WHERE (".TAPRODCA.".tacID IN {$this->subkategorie})  {$kuchnie} {$kewords} 
				   ORDER BY ".TAPRODUK.".tapNazwa ";
			$result = tools::__adodb('getAll',$query,true,$this->db);
		}
		return $result;
	}
	
	protected function pobierzPozostaleListeLokalizacjeProduktuZBazy($tapID,$tacID){
		$query = "SELECT 
				".TAPRODUK.".tapNazwa,
				".TAPRODUK.".tapLogo,
				".TAPRODCA.".tapID,
				".TAPRODCA.".tacID,
				".TAPRODCA.".tauID,
				".TAPRODCA.".tapLokalizacja,
				".TAPRODCA.".tapAdres
			FROM ".TAPRODUK."
			INNER JOIN ".TAPRODCA." ON (".TAPRODUK.".tapID=".TAPRODCA.".tapID)
		WHERE
		  (".TAPRODCA.".tapID = '{$tapID}' AND ".TAPRODCA.".tacID != '{$tacID}')";
  
		$result = tools::__adodb('getAll',$query,true,$this->db);	
		return $result;
		
	}
	
	protected function pobierzListeProduktowPromocyjnychSklepu($tacID__ = null){
		global $config;
		$tacIDFiltr = (!is_null($tacID__)) ? " a.tacID = '".tools::__mres($tacID__)."' AND " : "";
		$query = "SELECT a.*,	b.* ,( a.tapCena+(a.tapCena*a.tapStawVat)/100) AS tapCenaBrutto,
			(a.tapCenaPromocyjna+(a.tapCenaPromocyjna*a.tapStawVat)/100) AS tapCenaBruttoPromocyjna 
			FROM ".TAPRODUK." a LEFT JOIN ".TAPRODLA." b ON b.tapID = a.tapID 
			WHERE {$tacIDFiltr} b.tapLang = '{$config->__lang}' 
				AND a.tapCzyPromocja = 1 ORDER BY a.tapCena  DESC ";
		
		$produkty =  tools::__adodb('getAll',$query,true,$this->db);	
		
		return $produkty;	
	}
	
	protected function pobierzJedenProdukt($tapID__,$tacID__){
		global $config;
		$lang = (is_null($lang_)) ? $config->__lang : $lang_;	
		
		$query = "SELECT 	
				  ".TAPRODUK.".tapNazwa,		  
				  ".TAPRODUK.".tapStatus,
				  ".TAPRODUK.".tapLp,
				  ".TAPRODUK.".tapLogo,
				  ".TAPRODCA.".*
				FROM
				 ".TAPRODUK." LEFT JOIN ".TAPRODCA." ON (".TAPRODUK.".tapID=".TAPRODCA.".tapID)
				WHERE ".TAPRODUK.".tapID = ".tools::__mres($tapID__)." AND  ".TAPRODCA.".tacID = ".tools::__mres($tacID__); 
			$oferta =  tools::__adodb('getRow',$query,true,$this->db);
			return $oferta;
	}
	
	protected function pobierzJedenProduktDoEdycji(){
		global $config;
		$q__ = "SELECT a.*, b.*, d.tacNazwa, d.tacOpis FROM ".TAPRODUK." a 
			LEFT JOIN ".TAPRODCA." b ON b.tapID = a.tapID 
			LEFT JOIN ".TACATEGO." c ON c.tacID = b.tacID 
			LEFT JOIN ".TACATELA." d ON d.tacID = b.tacID
			WHERE a.tapID = '{$this->tapID}' AND d.tacLang = '{$config->__lang}'";
		$result = tools::__adodb('getRow',$q__,true,$this->db);
		
		return (is_array($result)) ? $result : false;
	}
	
	/**
	 * zapisuje zawartośc koszyka do bazy tranzakcji
	 * @param $tranzakacjaID
	 * @param $koszyk
	 * @return unknown_type
	 */
	protected function zapiszNowaTranzakcjeDoBazy($tranzakacjaID,$koszyk){
		if(is_array($koszyk) && !empty($koszyk)){
		 
			$q = "INSERT INTO ".TASPRZED." (tasID,tasControl) VALUES (NULL,'{$tranzakacjaID}')";
			$result = tools::__adodb('exec',$q,true,$this->db);
			
			$tasID_ = $this->db->lastInsertId();
						
			foreach ($koszyk as $row => $prod) {
				$q = "INSERT INTO ".TASPRZSZ." (tasID,tacID,tapID,tacNazwa,tapNazwa,tapIlosc,tapCena,tapWaluta,tapJednostka,tapSymbol,tapPhotoURL) 
				VALUES ('{$tasID_}','{$prod['KategoID']}','{$prod['ProdukID']}','{$prod['KategoNazwa']}'
				,'{$prod['ProdukNazwa']}','{$prod['ProdukIlosc']}','{$prod['ProdukCena']}','{$prod['ProdukWaluta']}'
				,'{$prod['ProdukJedn']}','{$prod['ProdukSymbol']}','{$prod['ProdukPhoto']}')";
				$result = tools::__adodb('exec',$q,true,$this->db);
			}
		}
	}
	
	protected function zapiszPotwierdzenieDoBazy(){
		global $config;
		$q = "UPDATE ".TASPRZED." SET 
			tasStatus = ".tools::__mres($config->status).",
			tasT_id = ".tools::__mres($config->t_id).",
			tasAmount = ".tools::__mres($config->amount).",
			tasEmail = ".tools::__mres($config->email).",
			tasService = ".tools::__mres($config->service).",
			tasCode = ".tools::__mres($config->code).",
			tasUsername = ".tools::__mres($config->username).",
			tasPassword = ".tools::__mres($config->password).",
			tasT_status = ".tools::__mres($config->t_status).",
			tasDescription = ".tools::__mres($config->description).",
			tasP_info = ".tools::__mres($config->p_info).",
			tasP_email = ".tools::__mres($config->p_email).",
			tasT_date = ".tools::__mres($config->t_date).",
			tasMd5 = ".tools::__mres($config->md5)."
			WHERE tasControl = '{$config->control}'";
		$result = tools::__adodb('exec',$q,true,$this->db);
	}
	
}